﻿
// 项目展示地址:"http://www.ddxc.org/"
// 如果你有什么好的建议或者觉得可以加什么功能，请加QQ群：104390185大家交流沟通
// 项目展示地址:"http://www.yoyocms.com/"
//博客地址：http://www.cnblogs.com/wer-ltm/
//代码生成器帮助文档：http://www.cnblogs.com/wer-ltm/p/5777190.html
// <Author-作者>角落的白板笔</Author-作者>
// Copyright © YoYoCms@中国.2017-11-22T22:16:56. All Rights Reserved.
//<生成时间>2017-11-22T22:16:56</生成时间>
using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Andons;
using KDS.Mes.Mes.Andons.Dtos;
using KDS.Mes.Mes.Parts.Dtos;

using KDS.Mes.Web.Controllers;
using KDS.Mes.Web.Models.ANDON;
using KDS.Mes.Web.Models.StopEquipments;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class Reports_UPHController : MesControllerBase
    {
        string ConnectionString = "Data Source=192.168.201.3;Initial Catalog=KDS.Mes;Persist Security Info=True;User ID=sa;Password=Passw0rd!";
   
        private readonly IANDONAppService _aNDONAppService;

        public Reports_UPHController(IANDONAppService aNDONAppService)
        {
            _aNDONAppService = aNDONAppService;
           
        }

     
        public async Task<ActionResult> Index()
        {
            var lines = new List<ComboboxItemDto>();

            lines.Insert(0, new ComboboxItemDto("", ""));
            lines.Insert(1, new ComboboxItemDto("1", "一线"));
            lines.Insert(2, new ComboboxItemDto("2", "二线"));
            lines.Insert(2, new ComboboxItemDto("3", "三线"));
            // var cartypes = new List<ComboboxItemDto>();
            //var getCartypesOutput = await _cartypeAppService.GetCartypes(new GetCartypeInput());
            //cartypes = getCartypesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.CartypeName)).ToList();

            //var styles = new List<ComboboxItemDto>();
            //var getStylesOutput = await _styleAppService.Getstyles(new GetStyleInput());
            //styles = getStylesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.StyleName)).ToList();

            //var seatPositions = new List<ComboboxItemDto>();
            //var getSeatPositionOutput = await _seatPositionAppService.GetSeatPositions(new GetSeatPositionInput());
            //seatPositions = getSeatPositionOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.SeatPositionName)).ToList();

            //cartypes.Insert(0, new ComboboxItemDto("", ""));
            //styles.Insert(0, new ComboboxItemDto("", ""));
            //seatPositions.Insert(0, new ComboboxItemDto("", ""));
            var model = new StopEquipmentsViewModel
            {
                FilterText = Request.QueryString["filterText"],
                Lines = lines,


            };


            return View(model);
            //var model = new GetPartInput {FilterText = Request.QueryString["filterText"]};
            //return View();
        }
        [DontWrapResult]
        public JsonResult GetReports(string date,string lineID)
        {
            string cmd = "EXEC	 [dbo].[sp_Report_Get_UPH]   @time = N'"+date+"', @LineId = "+lineID+"";
            return Sql(cmd);

        }
        [AbpMvcAuthorize(AppPermissions.Pages_Basic_Factorys_Create, AppPermissions.Pages_Basic_Factorys_Edit)]
        public async Task<PartialViewResult> CreateOrEditANDONModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _aNDONAppService.GetANDONForEditAsync(input);

            var viewModel = new CreateOrEditANDONModalViewModel(output);


            return PartialView("_CreateOrEditANDONModal", viewModel);
        }

        public JsonResult Sql(string command)
        {
            DataSet dt = new DataSet();
            SqlConnection Conn = new SqlConnection(ConnectionString);
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }

            Conn.ConnectionString = ConnectionString;
            Conn.Open();

            SqlCommand cmd = new SqlCommand(command, Conn);
            SqlDataReader reader = cmd.ExecuteReader();//执行SQL语句的真正查询了
            DataTable sum = new DataTable();


            dt.Tables.Add(sum);
            dt.Load(reader, LoadOption.OverwriteChanges, sum);     /*就这一句用DataTable.Load(DataReader)来把查询到的数据插入到DataTable中*/

            reader.Close();
            string test = DataTable2Json(dt.Tables[0]);

            return Json(test, JsonRequestBehavior.AllowGet);
        }
        public static string DataTable2Json(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("[");

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");

            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");

            return jsonBuilder.ToString();
        }

    }
}